package com.itheima.jobs;


import com.itheima.constant.RedisConstant;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.Set;

/**
 * @author:chengyu
 * @date:2023/12/21
 * @fun:自定义job，定时清理垃圾图片
 */
public class ClearImgJob {
    //操作redis数据库
    @Autowired
    private JedisPool jedisPool;

    public void clearImg() {
        // 根据redis中保存的集合进行差值计算，获取垃圾图片
        Set<String> set = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        if (set != null) {
            for (String picName : set) {
                //删除服务器图片
                QiniuUtils.deleteFileFromQiniu(picName);
                //redis集合删除图片名称
                jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES, picName);
                System.out.println("自定义任务执行，清理垃圾图片" + picName);
            }
        }
    }
}

































